cmake_minimum_required(VERSION 3.2)
project(matxscript_pytorch)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

IF (CMAKE_TORCH_CUDA_AVAILABLE)
    ADD_DEFINITIONS(-DMATX_ENABLE_TORCH_MODEL_AUTO_SYNCHRONIZATION_WITH_PREPROCESS)
ENDIF()

ADD_DEFINITIONS(-DMATXSCRIPT_PYTHON_MODE)

IF (NOT CMAKE_MATX_COMPILE_FLAGS)
    MESSAGE(STATUS "CMAKE_MATX_COMPILE_FLAGS has not been set, use setup.py to install.")
    RETURN(-1)
ENDIF()
MESSAGE(STATUS "MatxScript Compile Flags: ${CMAKE_MATX_COMPILE_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_MATX_COMPILE_FLAGS}")

IF (NOT CMAKE_MATX_LINK_FLAGS)
    MESSAGE(STATUS "CMAKE_MATX_LINK_FLAGS has not been set, use setup.py to install.")
    RETURN(-1)
ENDIF()
MESSAGE(STATUS "MatxScript Link Flags: ${CMAKE_MATX_LINK_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_MATX_LINK_FLAGS}")

FOREACH(TORCH_INC ${CMAKE_TORCH_INCLUDES})
    INCLUDE_DIRECTORIES(${TORCH_INC})
ENDFOREACH()

FOREACH(TORCH_LIB ${CMAKE_TORCH_LIB})
    LINK_DIRECTORIES(${TORCH_LIB})
ENDFOREACH()

INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/)

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O2 -std=c++14 -fPIC -w -fpermissive -pthread")
MESSAGE(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")

FILE(GLOB_RECURSE MATX_TORCH_SRCS RELATIVE ${CMAKE_SOURCE_DIR}  "src/torch_*.cc" "src/tensor_*.cc")

SET(BUILD_TARGET matx_pytorch+torch${CMAKE_TORCH_VERSION})
ADD_LIBRARY(${BUILD_TARGET} SHARED ${MATX_TORCH_SRCS})
# Link against LibTorch
TARGET_LINK_LIBRARIES(${BUILD_TARGET} PUBLIC torch)
TARGET_LINK_LIBRARIES(${BUILD_TARGET} PUBLIC torch_cpu)
TARGET_LINK_LIBRARIES(${BUILD_TARGET} PUBLIC c10)
